package com.example.sqlitedemo;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataBaseHelper extends SQLiteOpenHelper {
	public static final int DATABASE_VERSION = 1;
	public static final String DATABASE_NAME = "Demo.sqlite";
	public static final String tag = "DataBaseHelper";
	private static final boolean D = false;
	
	public static final String TABLE_NAME = "Person";
	public static final String COLUMN_NAME_FIRSTNAME = "firstname";
	public static final String COLUMN_NAME_LASTNAME = "lastname";
	public static final String COLUMN_NAME_ID = "id";
	
	private final Context myContext;
	private final String myPath;
	
	public DataBaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		
		myPath = context.getDatabasePath(DATABASE_NAME).getPath();
		
		myContext = context;
		
		boolean b = checkDataBase();
		
		if(b) {
			if(D) Log.d(tag, "Database exists on " + myPath);
		} else {
		}	
			this.getWritableDatabase();
			
			try {
				copyDataBase();
			} catch (IOException e) {
				if(D) Log.d(tag, "Catched: " + e);
			}
		
		
		
		
		
	}
	
	public DataBaseHelper(Context context, boolean mode) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		
		myPath = context.getDatabasePath(DATABASE_NAME).getPath();
		
		myContext = context;
		
		boolean b = checkDataBase();
		
		if(b) {
			if(D) Log.d(tag, "Database exists on " + myPath);
		} else {
		}	
			this.getWritableDatabase();
			
			try {
				copyDataBase();
			} catch (IOException e) {
				if(D) Log.d(tag, "Catched: " + e);
			}
		
		
		
		
		
	}

	/**
	 * Check if the database already exist to avoid re-copying the file each time 
	 * you open the application.
	 * @return true if it exists, false if it doesn't
	 */
	private boolean checkDataBase(){

	    SQLiteDatabase checkDB = null;

	    try{
	        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

	    }catch(SQLiteException e){

	    	if(D) Log.d(tag, "Database doesn't exist");

	    }

	    if(checkDB != null){

	        checkDB.close();

	    }

	    return checkDB != null ? true : false;
	}
	
	/**
	 * Copies your database from the local assets-folder to the just created empty database in the
	 * system folder, from where it can be accessed and handled.
	 * This is done by transferring byte stream.
	 * */
	private void copyDataBase() throws IOException {

	    /* Open the local db as the input stream */
	    InputStream myInput = myContext.getAssets().open(DATABASE_NAME);

	    // Path to the just created empty db
	    //String outFileName = DB_PATH + DB_NAME;
	    if(D) Log.d(tag, "Get input location");

	    //Open the empty db as the output stream
	    OutputStream myOutput = new FileOutputStream(myPath);

	    if(D) Log.d(tag, "Get output location");

	    //transfer bytes from the input file to the output file
	    byte[] buffer = new byte[2048];
	    int length;
	    while ((length = myInput.read(buffer))>0){
	        myOutput.write(buffer, 0, length);
	    }

	    //Close the streams
	    myOutput.flush();
	    myOutput.close();
	    myInput.close();

	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}
}
